﻿using Itech_Run.Domain.Shared.Const;
using Itech_Run.Domain.Shared.Exception;
using Itech_Run.Domain.Shared.Localization.Back;
using Itech_Run.Sys.Domain.Entity;
using Microsoft.Extensions.Localization;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Itech_Run.Sys.Domain.Manager
{
    public class Sys_DictionaryManager : DomainService
    {
        private readonly IRepository<Sys_Dictionary, string> _dictionaryRepository;
        private readonly IStringLocalizer<BackResource> _localizer;
        public Sys_DictionaryManager(IRepository<Sys_Dictionary, string> dictionaryRepository, IStringLocalizer<BackResource> localizer)
        {
            _dictionaryRepository = dictionaryRepository;
            _localizer = localizer;
        }

        public async Task Check_UniqueAsync(Sys_Dictionary entity)
        {
            await Check_UniqueCodeTypeCodeAsync(entity.Id, entity.CodeType, entity.Code);
        }

        private async Task Check_UniqueCodeTypeCodeAsync(string id, string codeType, string code)
        {
            if ((await _dictionaryRepository.GetQueryableAsync()).Where(p => p.CodeType == codeType && p.Code == code && p.Id != id).Any())
                throw new CustomFriendlyException(BackErrorConst.DictionaryCodeTypeAndCodeAlreadyExists, _localizer[BackErrorConst.DictionaryCodeTypeAndCodeAlreadyExists]);
        }
    }
}
